﻿using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlayCardCapture
{
    public partial class DualCapture : Form
    {
        private Capture capture1;
        private Capture capture2;
        private int playerCount;
        private int cardPerPlayer;
        private bool incapture;
        private int frameCount;
        private int[] indexArray;
        private bool[] inCap2;
        private int capindex;
        private ImageManager manager;
        private ResultForm resultForm;
        private VideoWriter writer1;
        private VideoWriter writer2;
        private int stopCount;

        public DualCapture()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            capture1 = new Capture(0);
            capture1.Grab();
            capture2 = new Capture(1);
            capture2.Grab();
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Image<Bgr, Byte> frame1 = capture1.QueryFrame();
            imageBox.Image = frame1;
            Image<Bgr, Byte> frame2 = capture2.QueryFrame();
            imageBox1.Image = frame2;
            
            if (incapture)
            {
                writer1.WriteFrame(frame1);
                writer2.WriteFrame(frame2);
                frameCount++;
                if (stopCount > 0)
                {
                    if (stopCount > 10)
                    {
                        writer1.Dispose();
                        writer2.Dispose();
                        manager = new DualFileManager("record1.avi","record2.avi", playerCount, cardPerPlayer, indexArray,inCap2);
                        resultForm = new ResultForm();
                        resultForm.init(manager);
                        resultForm.Show();
                        incapture = false;
                        button1.Enabled = true;
                        button2.Enabled = false;
                        stopCount = 0;
                    }
                    else
                        stopCount++;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            playerCount = int.Parse(comboBox1.Text);
            cardPerPlayer = int.Parse(comboBox2.Text);
            frameCount = 0;
            capindex = 0;
            indexArray = new int[playerCount * cardPerPlayer];
            inCap2 = new bool[playerCount * cardPerPlayer];
            writer1 = new VideoWriter("record1.avi", CvInvoke.CV_FOURCC('D', 'I', 'V', 'X'), 25, 720, 576, true);
            writer2 = new VideoWriter("record2.avi", CvInvoke.CV_FOURCC('D', 'I', 'V', 'X'), 25, 720, 576, true);
            incapture = true;
            button1.Enabled = false;
            button2.Enabled = true;
            stopCount = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            incapture = false;
            button1.Enabled = true;
            button2.Enabled = false;
        }

        private void DualCapture_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (button2.Enabled)
            {

                if (e.KeyChar == 'k' || e.KeyChar == 'd')
                {
                    bool flag = false;
                    if (e.KeyChar == 'k')
                        flag = true;
                    if (capindex <= playerCount * cardPerPlayer - 1)
                    {
                        indexArray[capindex] = frameCount;
                        inCap2[capindex] = flag;
                        capindex++;
                    }
                    if (capindex == playerCount * cardPerPlayer - 1)
                    {
                        stopCount++;
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (resultForm != null)
                resultForm.Show();
        }

        private void DualCapture_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
